package com.gxa.learning.pc.controller;

import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.pojo.CommentVo;
import com.gxa.learning.core.util.CommentTreeUtil;
import com.gxa.learning.pc.service.MainService;
import com.gxa.learning.pc.service.ProjectRemarkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author YXQ
 * @date 2021/12/16 8:38
 */
@RestController
@RequestMapping("/remark")
@Api(tags = "给项目的评论准备的接口")
public class ProjectRemarkController {

    @Autowired
    ProjectRemarkService projectRemarkService;

    @GetMapping("/comment")
    @ApiOperation("评论与回复的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "userId: 当前登录用户的id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "projectId",value = "projectId: 评论的项目的id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "remark",value = "remark: 评论的内容",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "pId",value = "pId: 直接评论->pId为0,回复->pid为一级评论的编号",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "reply",value = "reply: 被回复的人",dataType = "string",paramType = "query")
    })
    public Map<String, Object> comment(Long userId, Long projectId, String remark, Long pId, String reply) {
        projectRemarkService.comment(userId,projectId,remark,pId,reply);
        JsonResult jsonResult = new JsonResult();
        // 设置返回的数据
        jsonResult.setCode("1");
        jsonResult.setMsg("评论发布成功");
        // 返回给前端
        return jsonResult.getDataMap();
    }

    @GetMapping("/likeComment")
    @ApiOperation("点赞评论与回复的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "userId: 当前评论用户的id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "projectId",value = "projectId: 当前培训的id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "remarkId",value = "remarkId: 点赞的评论的id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "isLike",value = "isLike: 是否点赞 1点赞 0取消点赞",dataType = "Integer",paramType = "query")
    })
    public Map<String, Object> likeComment(Long userId, Long projectId, Long remarkId, Integer isLike) {
        projectRemarkService.likeComment(userId,projectId,remarkId,isLike);
        JsonResult jsonResult = new JsonResult();
        // 设置返回的数据
        jsonResult.setCode("1");
        jsonResult.setMsg("操作成功");
        // 返回给前端
        return jsonResult.getDataMap();
    }

    @GetMapping("/getCommentList")
    @ApiOperation("获取评论与回复的列表的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "userId: 当前评论用户的id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "projectId",value = "projectId: 当前培训的id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "page: 页码",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "size: 每页的记录数",dataType = "int",paramType = "query")
    })
    public Map<String, Object> getCommentList(Long userId, Long projectId,Integer page,Integer size) {
        // 需要给前端的数据，用户编号，用户头像，用户发表评论的时间，评论的内容，点赞，谁回复了谁
        // 一级，二级
        List<CommentVo> commentList = projectRemarkService.getCommentList(userId, projectId);
        // 将拿到的集合转成树状结构
        List<CommentVo> oneMenu = CommentTreeUtil.getOneMenu(commentList);
        int size1 = oneMenu.size();
        // 给前端返回的数据
        List<CommentVo> newOneMenu = new ArrayList<>(3);
        // 给前端的返回数据
        JsonResult jsonResult = new JsonResult();
        // 分页
        if (page != null && !TrainPlanController.ZERO.equals(page)) {
            if (size != null && !TrainPlanController.ZERO.equals(size)) {
                // page和size都不为null或者0时
                if (size1 == 0) {
                    jsonResult.setData(oneMenu);
                } else {
                    // 遍历总的结果集，把分页的部分数据传入分页后的集合
                    for (int i = 0; i < size; i++) {
                        if ((page - 1) * size + i >= size1) {
                            break;
                        }
                        // 从(page - 1) * size开始的size或者小于size条数据装入新的集合中
                        newOneMenu.add(oneMenu.get((page - 1) * size + i));
                    }
                    // 传给前端
                    jsonResult.setData(newOneMenu);
                }
            }
        } else {
            jsonResult.setData(oneMenu);
        }
        // 设置返回的数据
        jsonResult.setCode("1");
        jsonResult.setMsg("获取列表成功");
        jsonResult.put("count",oneMenu.size());
        // 返回给前端
        return jsonResult.getDataMap();
    }
}
